<?php
tsload(APPS_PATH.'/admin/Lib/Action/AdministratorAction.class.php');
class EquipmentAction extends AdministratorAction{

public $pageTitle = array();
	
	/**
	 * 初始化，初始化页面表头信息，用于双语
	 */
	public function _initialize() {
		$this->pageTitle['index'] = '所有设备';
		
		$this->pageTitle['addEquipment'] = '添加设备';
	

		parent::_initialize();
	}

	/**
	 * 设备列表
	 */
	public function index()
	{
		$_REQUEST['tabHash'] = 'index';
		// 初始化用户列表管理菜单
		$this->_initUserListAdminMenu('index');
		
		$listData = $this->_getUserList('20', $map, 'index');
		
		//$this->pageButton[] = array('title'=>'设备搜索','onclick'=>"admin.fold('search_form')");
		$this->displayList($listData);
	}

	/**
	 * 用户管理 - 待审列表
	 */
	public function pending() {
		$_REQUEST['tabHash'] = 'pending';
		// 初始化审核列表管理菜单
		$this->_initUserListAdminMenu('pending');
		// 数据的格式化与listKey保持一致
		$listData = $this->_getUserList(20, array('is_audit' => 0,'is_del'=>'0'), 'pending');
		// 列表批量操作按钮
		$this->pageButton[] = array('title'=>L('PUBLIC_SEARCH_USER'),'onclick'=>"admin.fold('search_form')");
		$this->pageButton[] = array('title'=>L('PUBLIC_AUDIT_USER_SUCCESS'),'onclick'=>"admin.auditUser('',1)");

		$this->displayList($listData);
	}

	/**
	 * 用户管理 - 禁用列表
	 */
	public function dellist() {
		$_REQUEST['tabHash'] = 'dellist';
		// 初始化禁用列表管理菜单
		$this->_initUserListAdminMenu('dellist');
		// 数据的格式化与listKey保持一致
		$listData = $this->_getUserList(20, array('is_del'=>'1'), 'dellist');
		// 列表批量操作按钮
		$this->pageButton[] = array('title'=>L('PUBLIC_SEARCH_USER'),'onclick'=>"admin.fold('search_form')");
		$this->pageButton[] = array('title'=>L('PUBLIC_RECOVER_ACCOUNT'),'onclick'=>"admin.rebackUser()");

		$this->displayList($listData);
	}

	

	/**
	 * 初始化用户列表管理菜单
	 * @param string $type 列表类型，index、pending、dellist
	 */
	private function _initUserListAdminMenu($type) {
		// tab选项
		$this->pageTab[] = array('title'=>'设备列表','tabHash'=>'index','url'=>U('admin/Equipment/index'));
		$this->pageTab[] = array('title'=>'添加设备','tabHash'=>'addEquipment','url'=>U('admin/Equipment/addEquipment'));
		// 搜索选项的key值
		// $this->searchKey = array('uid','uname','email','sex','department','user_group',array('ctime','ctime1'));
		//$this->searchKey = array('eid','e_name','status','remark');
		// 针对搜索的特殊选项
		$this->opt['status'] = array('0'=>'否','1'=>'是');
		// 列表key值 DOACTION表示操作
		switch(strtolower($type)) {
			case 'index':
			case 'dellist':
				$this->pageKeyList = array('eid','e_name','logo','description','status','remark','DOACTION');
				break;
			case 'pending':
				$this->pageKeyList = array('uid','uname','location','ctime','reg_ip','DOACTION');
				break;
		}
		
/*		if(!empty($_POST['_parent_dept_id'])) {
			$this->onload[] = "admin.departDefault('".implode(',', $_POST['_parent_dept_id'])."','form_user_department')";
		}*/
	}

	/**
	 * 解析用户列表数据
	 * @param integer $limit 结果集数目，默认为20
	 * @param array $map 查询条件
	 * @param string $type 格式化数据类型，index、pending、dellist
	 * @return array 解析后的用户列表数据
	 */
	private function _getUserList($limit = 20, $map = array(), $type = 'index') {
		// 设置列表主键
		$this->_listpk = 'eid';
		// 取用户列表
		$listData['data'] = M('Equipment')->select();
		// 数据格式化
		foreach ($listData['data'] as $k=>$v){
			if($v['logo']){
				$image = D('attach')->where('attach_id='.$v['logo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$image_url = getImageUrl($image_url);
				$listData['data'][$k]['logo'] = "<img style='height:50px;' src='".$image_url."'"." />";
			}
			
			$listData['data'][$k]['status'] = ($v['status'] == 1) ? '是':'否';
			$listData['data'][$k]['DOACTION'] = '<a href="'.U('admin/Equipment/editEquipment',array('tabHash'=>'editEquipment','eid'=>$v['eid'])).'">'.L('PUBLIC_EDIT').'</a> - ';
			$listData['data'][$k]['DOACTION'] .= $v['status'] == 0 ? '<a href="javascript:void(0)" onclick="admin.rebackEquipment(\''.$v['eid'].'\')">'.'启用'.'</a> - ' : '<a href="javascript:void(0)" onclick="admin.disEquipment(\''.$v['eid'].'\')">'.'关闭'.'</a> - ';
			$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.trueDelEquipment(\''.$v['eid'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
		
		}
		
		
		return $listData;
	}
	
	/**
	 * 删除设备
	 */
	public function trueDelEquipment(){
		if(empty($_POST['id'])) {
			$return['status'] = 0;
			$return['data'] = '';
			exit(json_encode($return));
		}
		$Equipment = M('Equipment');
		$Equipment->eid = $_POST['id'];
		$result = $Equipment->delete();
		if(!$result) {
			$return['status'] = 0;
			$return['data'] = L('PUBLIC_REMOVE_COMPLETELY_FAIL');				// 操作失败
		} else {
			// 关联删除用户其他信息，执行删除用户插件.
			$return['status'] = 1;
			$return['data'] = L('PUBLIC_REMOVE_COMPLETELY_SUCCESS');			// 操作成功
		}
		exit(json_encode($return));
	
	}
	/**
	 * 添加设备
	 */
	public function addEquipment() {
		// 初始化用户列表管理菜单
		$this->_initUserListAdminMenu();
		
        $this->pageKeyList = array('e_name','logo','description','status','remark');
       
      
		$this->opt['status'] = array('1'=>'启用','0'=>'关闭');
		
		
	
		$this->savePostUrl = U('admin/Equipment/doAddequipment');
        $this->notEmpty = array('ename','status');
        $this->onsubmit = 'admin.addEquipmentSubmitCheck(this)';

		$this->displayConfig();
	}

	/**
	 * 添加设备操作
	 */
	public function doAddequipment() {
		$Equipment = M('Equipment');
		$Equipment->e_name = trim($_POST['e_name']);
		$Equipment->status = $_POST['status'];
		$Equipment->logo = $_POST['logo'];
		$Equipment->description = $_POST['description'];
		$Equipment->remark = $_POST['remark'];
		//dump($Equipment);exit;
		//dump($Equipment['ename']);exit;
		// 检查map返回值，有表单验证
		$result = $Equipment->add();
		if($result) {
			$this->assign('jumpUrl', U('admin/Equipment/index'));
			$this->success(L('PUBLIC_ADD_SUCCESS'));
		} else {
			$this->assign('jumpUrl', U('admin/Equipment/index'));
			$this->success('添加失败');
		}
	}

	/**
	 * 编辑设备信息页面
	 */
	public function editEquipment(){
		// 初始化用户列表管理菜单
		$this->_initUserListAdminMenu();
		// 列表key值 DOACTION表示操作
		
		$this->pageKeyList = array('eid','e_name','logo','description','status','remark');
		
		// 字段选项配置
		$this->opt['status'] = array('0'=>'否','1'=>'是');
		
		
		$eid = intval($_REQUEST['eid']);
		//$userInfo  = model('User')->getUserInfo($uid);
		$info = M("Equipment")->where("eid = $eid")->select();
		
		if(!$info[0]){
			$this->error(L('PUBLIC_GET_INFORMATION_FAIL'));
		}

		$this->assign('pageTitle','设备信息编辑');
		$this->savePostUrl = U('admin/Equipment/doUpdateEquipment');

		// $this->notEmpty = array('email','uname','department_id');
		$this->notEmpty = array('mac_id','e_name','status','remark');
       	$this->onsubmit = 'admin.checkEquipment(this)';

		$this->displayConfig($info[0]);
	}

	/**
	 * 更新设备信息
	 */
	public function doUpdateEquipment() {
		$Equipment = M('Equipment');
		$Equipment->eid = $eid = intval($_POST['eid']);
		$Equipment->logo = intval($_POST['logo']);
		$Equipment->description = $_POST['description'];
		$Equipment->ename = $ename = intval($_POST['ename']);
		$Equipment->status = $status = intval($_POST['status']);
		$Equipment->remark = $remark = $_POST['remark'];
		//dump($Equipment);exit;
		// 验证用户名称是否重复
		$check_eid = $Equipment->where('eid='.$eid)->getField('eid');
		if($Equipment->save()){
			$this->success(L('PUBLIC_SYSTEM_MODIFY_SUCCESS'));
		}else{
			$this->error(L('PUBLIC_ADMIN_OPRETING_ERROR'));
		}
		
	
	}

	
	/**
	 * 设备关闭使用操作
	 * @return json 操作后的JSON数据
	 */
	public function disEquipment() {
		if(empty($_POST['id'])) {
			$return['status'] = 0;
			$return['data'] = '';
			exit(json_encode($return));
		}

		//$result = model('User')->deleteUsers(intval($_POST['id']));
		$Equipment = M('Equipment');
		$Equipment->status = 0;
		$Equipment->eid  =$_POST['id'];
		$result = $Equipment->save();
		if(!$result) {
			$return['status'] = 0;
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');				// 操作失败
		} else {
			// 关联删除用户其他信息，执行删除用户插件.
			$return['status'] = 1;
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');			// 操作成功
			
		}
		exit(json_encode($return));
	}

	/**
	 * 设备恢复启用操作
	 * @return json 操作后的JSON数据
	 */
	public function rebackEquipment() {
		if(empty($_POST['id'])) {
			$return['status'] = 0;
			$return['data'] = '';
			exit(json_encode($return));
		}
		$Equipment = M('Equipment');
		$Equipment->status = 1;
		$Equipment->eid  =$_POST['id'];
		$result = $Equipment->save();
		if(!$result) {
			$return['status'] = 0;
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');				// 操作失败
		} else {
			// 关联删除用户其他信息，执行删除用户插件.
			$return['status'] = '1';
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');			// 操作成功
		}
		exit(json_encode($return));
	}

	
}